/**************************************************************************************
*
*	Copyright (C) 2009 - 2012 Brent Andrew O. Chua - See LICENSE.txt for more info.
*		
*	Filename	-	LockFreeQueue.h
*
*	Description -	Warning! This only works for x86 platforms.
*
*	Comments	-	
*
*	Modification History:
*			Name			   Date					Description
*			MrCodeSushi	-	08/09/2012	-	Creation of this file
**************************************************************************************/
#include "PulseSTD.h"
#include "Allocators.h"
#include "AtomicInt.h"

namespace Pulse
{
	template < typename T >
	class LockFreeQueue
	{
	public:

		LockFreeQueue( void );

		LockFreeQueue( SIZE_T size );

		~LockFreeQueue( void );

		void Initialize( SIZE_T size );

		BOOL Enqueue( T &elem );

		T & Dequeue( void );

	private:

		void Cleanup( void );

	private:

		struct Node
		{
			Node( void ) { }

			Node ( UINT _key, T &_pElem )
				: key( _key ), elem( _pElem )
			{ 
			}

			union
			{
				struct
				{
					UINT32 key;
					T &elem;
				};
			};
			INT64 value;
		};
		
		SIZE_T m_size;
		SIZE_T m_bucketMask;
		Node *m_pBuffer;
		AtomicInt m_readIndex;
		AtomicInt m_writeIndex;
	};

}

#include "LockFreeQueue.hpp"
